import config from "@/config";

const storage = {
    // localStorage
    data: {
        /**
         * 向 localStorage 设置值
         * @param key 键
         * @param value 值
         * @param dateTime 时长
         */
        set: (key, value, dateTime = 0) => {
            // 是否AES加密存储
            if (config.LOCAL_STORAGE_ENCRYPT === "AES")
                value = crypto.AES.encrypt(JSON.stringify(value), config.LOCAL_STORAGE_ENCRYPT_KEY)
            let cacheValue = {
                content: value,
                datetime: dateTime === 0 ? 0 : new Date().getTime() + dateTime * 1000
            }
            return localStorage.setItem(key, JSON.stringify(cacheValue))
        },
        /**
         * 从 localStorage 取出值
         * @param key 键
         * @returns {any|null} 值
         */
        get: (key) => {
            try {
                const value = JSON.parse(localStorage.getItem(key))
                if (value) {
                    let nowTime = new Date().getTime()
                    if (nowTime > value.datetime && value.datetime !== 0) {
                        localStorage.removeItem(key)
                        return null
                    }
                    // 解密
                    if (config.LOCAL_STORAGE_ENCRYPT === "AES")
                        value.content = JSON.parse(crypto.AES.decrypt(value.content, config.LOCAL_STORAGE_ENCRYPT_KEY))
                    return value.content
                }
                return null
            } catch (err) {
                return null
            }
        },
        /**
         * 从 localStorage 删除值
         * @param key 键
         */
        remove: (key) => {
            return localStorage.removeItem(key)
        },
        /**
         * 清空 localStorage
         */
        clear: () => {
            return localStorage.clear()
        }
    },
    // sessionStorage
    session: {
        /**
         * 向 sessionStorage 设置值
         * @param key 键
         * @param value 值
         */
        set: (key, value) => {
            return sessionStorage.setItem(key, JSON.stringify(value))
        },
        /**
         * 从 sessionStorage 取出值
         * @param key 键
         * @returns {*|string|null} 值
         */
        get: (key) => {
            let data = sessionStorage.getItem(key)
            try {
                data = JSON.parse(data)
            } catch (err) {
                return null
            }
            return data
        },
        /**
         * 从 sessionStorage 删除值
         * @param key 键
         */
        remove: (key) => {
            return sessionStorage.removeItem(key)
        },
        /**
         * 清空 sessionStorage
         */
        clear: () => {
            return sessionStorage.clear()
        }
    },
    // cookie
    cookie: {
        /**
         * 向 cookie 设置值
         * @param key 键
         * @param value 值
         * @param config 配置项
         */
        set: (key, value, config = {}) => {
            let cfg = {
                expires: null, path: null, domain: null, secure: false, httpOnly: false, ...config
            }
            let cookieStr = `${key}=${escape(value)}`
            if (cfg.expires) {
                let exp = new Date()
                exp.setTime(exp.getTime() + parseInt(cfg.expires) * 1000)
                cookieStr += `;expires=${exp.toGMTString()}`
            }
            if (cfg.path) {
                cookieStr += `;path=${cfg.path}`
            }
            if (cfg.domain) {
                cookieStr += `;domain=${cfg.domain}`
            }
            document.cookie = cookieStr
        },
        /**
         * 从 cookie 取出值
         * @param key 键
         * @returns {null|string} 值
         */
        get: (key) => {
            let arr = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)"))
            if (arr != null) {
                return unescape(arr[2])
            } else {
                return null
            }
        },
        /**
         * 从 cookie 删除值
         * @param key 键
         */
        remove: (key) => {
            let exp = new Date()
            exp.setTime(exp.getTime() - 1)
            document.cookie = `${key}=;expires=${exp.toGMTString()}`
        }
    }
}

export default storage;